package top.hg.tool.modules.car.notice;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import top.hg.tool.modules.car.constant.RedisKeyConstant;

import java.util.Date;
import java.util.List;

/**
 * @Author: skh
 * @Date: 2019/7/16 16:50
 * @Description:
 */
@Service
@Slf4j
public class NoticeService extends ServiceImpl<NoticeDao, NoticeEntity> {
    public void saveNotice(NoticeEntity noticeEntity) {
        noticeEntity.setCreateTime(new Date());
        noticeEntity.setStatus(1);
        this.insert(noticeEntity);
    }

    @Cacheable(cacheNames=RedisKeyConstant.car_notice_type,key = "#type")
    public List<NoticeEntity> getNoticeList(String type) {
        log.info("从数据库获取通知列表");
        EntityWrapper<NoticeEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("status", 1);
        wrapper.orderBy("rank", false);
        return this.selectList(wrapper);
    }


    @CacheEvict(cacheNames=RedisKeyConstant.car_notice_type,allEntries = true)
    public void clearNoticeCache() {
        log.info("清除通知缓存成功");
    }
}
